﻿$('#mainmanu a').click(function (e) {
    e.preventDefault();
    $(this).tab('show');
})

$(document).ready(function () {

    $("#MainContent_txtNumberOfNight").keydown(function (event) {
        var skipChar = [8, 9, 13];

        if ($.inArray(event.keyCode, skipChar) < 0 && (event.keyCode < 48 || event.keyCode > 57)) {
            event.preventDefault();
        }
    });

    calculateRoomCharges();

    var today = new Date();
    var minDate = new Date(today.setDate(today.getDate() + 3));

    $("#MainContent_txtArrivalDate").datepicker({
        dateFormat: "dd/mm/yy",
        minDate: minDate,
        onSelect: function () {
            changeReturnDate();

            var arrival_date = $(this).datepicker("getDate");
            $("#CalArrival").datepicker("setDate", arrival_date);


        }
    }).val($.datepicker.formatDate("dd/mm/yy", minDate)).keydown(function (event) {
        event.preventDefault();
    });


    $("#MainContent_txtDepatureDate").datepicker({
        dateFormat: "dd/mm/yy",
        minDate: getReturnDate(),
        onSelect: function () {
            var dept_date = $(this).datepicker("getDate");
            $("#CalDeparture").datepicker("setDate", dept_date);
            updateNights();
        }
    }).val($.datepicker.formatDate("dd/mm/yy", getReturnDate()));


    $("#CalArrival").datepicker({
        minDate: minDate,
        onSelect: function (datetext) {

            var d = $(this).datepicker("getDate");

            $("#MainContent_txtArrivalDate").val($.datepicker.formatDate("dd/mm/yy", d));

            changeReturnDate();

        }
    });
    $("#CalDeparture").datepicker({
        minDate: getReturnDate(),
        onSelect: function (datetext) {

            var d = $(this).datepicker("getDate");

            $("#MainContent_txtDepatureDate").val($.datepicker.formatDate("dd/mm/yy", d));
            updateNights();
        }
    });

    $("#MainContent_txtNumberOfNight").change(function () {
        changeReturnDate();
    });

    $("#next_month").click(function (e) {
        $("a[data-handler=next]").trigger("click");
    });

    $("#prev_month").click(function (e) {
        $("a[data-handler=prev]").trigger("click");
    });

});

function getReturnDate() {

    var numOfNight = getNumberOfNight();

    var arrival_date = $("#MainContent_txtArrivalDate").datepicker("getDate");

    var departure_date = new Date(arrival_date.setDate(arrival_date.getDate() + numOfNight));

    return departure_date;
}

function getNumberOfNight(){
    var numOfNight = $("#MainContent_txtNumberOfNight").val();

    if (numOfNight.trim() == "") {
        numOfNight = 1;
    }
    else {
        numOfNight = parseInt(numOfNight);
    }
    return numOfNight;
}

function changeReturnDate() {
    var dep_date = getReturnDate();
    var arr_date = $("#MainContent_txtArrivalDate").datepicker("getDate");

    arr_date = new Date(arr_date.setDate(arr_date.getDate() + 1));

    $("#MainContent_txtDepatureDate").val($.datepicker.formatDate("dd/mm/yy",dep_date));
    $("#CalDeparture").datepicker("setDate", dep_date);
    $("#CalDeparture").datepicker("option", "minDate", arr_date);
}

function updateNights() {
    var arr_date = $("#CalArrival").datepicker("getDate");
    var dep_date = $("#CalDeparture").datepicker("getDate");

    var nights = 1;

    if (arr_date && dep_date) {
        nights = Math.floor((dep_date.getTime() - arr_date.getTime()) / 86400000); // ms per day
    } else {
        nights = 1;
    }

    $("#MainContent_txtNumberOfNight").val(nights);
}


function calculateRoomCharges() {

    var price = $(".normalPrice");
    var charges = $(".roomCharges");
    var rooms = $("#MainContent_txtNoOfRoom").val();
    var nights = $("#MainContent_txtNoOfNights").val();
    var discount = $("#MainContent_txtVoucherAmt").val();
    var totRate = $(".totRate");
    var i = 0;
    for (i; i < price.length; i++) {

        var cal_prices = $(price[i]).text() * nights * rooms;

        $("#" + charges[i].id).text(cal_prices);

        $("#" + totRate[i].id).text(cal_prices - discount);
    }

}